package top.tervest.easyshare.common.security.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import top.tervest.easyshare.dao.UserDao;
import top.tervest.easyshare.domain.entity.UserAuthInfo;

import java.util.List;
import java.util.Objects;


/**
 * @author ZhangNanNan
 * @create 2019-01-16 12:37
 */
@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        UserAuthInfo authUserInfo = userDao.getAuthUserInfo(s);
        if (Objects.isNull(authUserInfo)) {
            return null;
        }
        List<String> roles = userDao.listRole(s);
        authUserInfo.setRoles(roles);
        return new UserDetailsImpl(authUserInfo) ;
    }

}
